<?php

/**
 * 用户需求
 */

namespace app\api\model;


use app\common\lib\RespCode;
use custom\Token;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Model;

class UserDemand extends Model {

    const TYPE1 = 1; //不限
    const TYPE2 = 2;//只看小区
    const TYPE3 = 3;//只看商圈

    /**
     * 添加/编辑需求
     * @param $params
     * @return int|string
     * @throws Exception
     * @throws PDOException
     */
    public static function edit($params) {
        if ($params['type'] != self::TYPE2 && $params['type'] != self::TYPE3) {
            $params['type_id'] = 0;
        } else {
            if (empty($params['type_id'])) {
                throw new Exception('请选择小区或者商圈', RespCode::PARAMS_ERR);
            }
        }
        $data = [
            'user_id' => $params['user_id'],
            'city_code' => intval($params['city_code']),
            'area_code' => intval($params['area_code']),
            'price_section' => trim($params['p_key']),
            'area_section' => trim($params['a_key']),
            'type' => intval($params['type']),
            'type_id' => intval($params['type_id']),
            'itime' => time(),
        ];
        if (empty($params['id'])) {
            $count = Db::name("user_demand")->where('user_id', $params['user_id'])->count();
            if ($count >= 3) {
                throw new Exception('最多保存3条需求', RespCode::QUERY_ERR);
            } else {
                self::insertGetId($data);
                return true;
            }
        } else {
            self::where('id', $params['id'])->update($data);
            return true;
        }
    }

    /**
     * 展示需求小区/房源
     * @param $params
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function show($params) {
        $data = self::alias('ud')
            ->leftJoin('region r', 'r.city_code=ud.city_code')
            ->leftJoin('region r1', 'r1.city_code=ud.area_code')
            ->where('ud.user_id', $params['user_id'])
            ->field('r.name as city_name,ud.id,ud.type_id,ud.type,r1.name as area_name,ud.price_section,ud.city_code,ud.area_code,ud.area_section')
            ->select()
            ->all();
        if (empty($data)) {
            return [];
        } else {
            foreach ($data as $v) {
                if ($v['type'] == self::TYPE1) {
                    $v['list_typename'] = '不限';
                    $v['info_business_or_community'] = '';
                }
                if ($v['type'] == self::TYPE2) {
                    $v_name = HouseVillage::get_Name($v['type_id']);
                    $v['list_typename'] = "只看'" . $v_name . "'小区";
                    $v['info_business_or_community'] = $v_name;
                }
                if ($v['type'] == self::TYPE3) {
                    $a_name = BusinessArea::get_Name($v['type_id']);
                    $v['list_typename'] = "只看'" . $a_name . "'商圈";
                    $v['info_business_or_community'] = $a_name;
                }
                //列表展示市-区
                $v['list_title'] = $v['city_name'] . '-' . $v['area_name'];
                $v['info_city_name'] = $v['city_name'];
                $v['info_area_name'] = $v['area_name'];
                unset($v['city_name']);
                unset($v['area_name']);
                $price_section = explode('-', $v['price_section']);
                $v['list_price_section'] = IntervalConfig::getConfig_name($price_section[0], $price_section[1], 1);
                $v['info_city_code'] = $v['city_code'];
                $v['info_area_code'] = $v['area_code'];
                $v['info_price_section'] = $v['price_section'];
                $v['info_area_section'] = $v['area_section'];
                $v['info_type'] = $v['type'];
                $v['info_business_or_community_id'] = $v['type_id'];
                unset($v['type']);
                unset($v['city_code']);
                unset($v['area_code']);
                $ps = explode('-', $v['price_section']);
                $v['info_price_section_name'] = IntervalConfig::getConfig_name($ps[0], $ps[1], 1);
                $as = explode('-', $v['area_section']);
                $v['info_area_section_name'] = IntervalConfig::getConfig_name($as[0], $as[1], 2);
                unset($v['price_section']);
                unset($v['area_section']);
                unset($v['type_id']);
            }
            return $data;
        }
    }

    /**
     * 删除需求
     * @param $params
     * @return int
     * @throws Exception
     * @throws PDOException
     */
    public static function del($params) {
        return Db::name("user_demand")->where('user_id', $params['user_id'])->delete($params['id']);
    }

    /**
     * 获取单个需求
     * @param $id
     * @return array|PDOStatement|string|Model|null
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getOne($id) {
        return Db::name("user_demand")->where('id', $id)->find();
    }

    /**
     * 我的需求数量
     * @param $uid
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getMycount($uid) {
        return Db::name("user_demand")->where('user_id', $uid)->select();
    }

}
